package com.android.djlmapp.adapter.home;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.model.home.HomeFragmentInfo;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class HomeGrideAdapter extends BaseAdapter {
    private ArrayList<HomeFragmentInfo.ActionListBean> mList;
    private Activity activity;
    private boolean isHaveData = false; // 是否有数据
    private int[] mPhotoIds = {R.mipmap.home_hmkk, R.mipmap.home_hfcz,
            R.mipmap.home_kdrh, R.mipmap.home_gmsj, R.mipmap.home_fkcz,
            R.mipmap.home_bkhk, R.mipmap.home_tcqz, R.mipmap.home_yhyz,
            R.mipmap.home_qqcz, R.mipmap.home_pdyw, R.mipmap.home_kdyxd,
            R.mipmap.home_gdgn};

    private static final String[] names = new String[]{"号码开卡", "话费充值", "宽带融合", "购买手机",
            "补卡换卡", "废卡重置", "套餐迁转", "QQ充值", "派单业务", "宽带意向单", "用户验证", "更多功能"};

    private static final String[] actions = new String[]{"hmkk", "hfcz", "kdrh", "gmsj", "bkhk",
            "fkcz", "tcqz", "qqcz", "pdyw", "kdyxd", "yhyz", "gdgn"};

    public HomeGrideAdapter(Activity activity) {
        this.activity = activity;

    }

    public void setData(ArrayList<HomeFragmentInfo.ActionListBean> mList, boolean isHaveData) {
        this.mList = mList;
        this.isHaveData = isHaveData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (isHaveData) {
            return mList.size();
        } else {
            return mPhotoIds.length;
        }
    }

    @Override
    public Object getItem(int position) {
        if (isHaveData) {
            return mList.get(position);
        } else {
            return mPhotoIds[position];
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        View view = View.inflate(activity, R.layout.home_grid, null);
        ImageView iv_icon = view.findViewById(R.id.iv_icon);
        TextView tv_name = view.findViewById(R.id.tv_name);

        if (isHaveData) {
            HomeFragmentInfo.ActionListBean info = (HomeFragmentInfo.ActionListBean) getItem(position);
            Glide.with(activity).load(info.getIcon()).into(iv_icon);
            tv_name.setText(info.getName());
        } else {
            int icons = (int) getItem(position);
            Glide.with(activity).load(icons).into(iv_icon);
            tv_name.setText(names[position]);
        }


        return view;
    }
}
